package com.dsteiner.widgets.client;

import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.InlineHTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.controller.RateItController;
import com.dsteiner.widgets.client.controller.StarController;
import com.dsteiner.widgets.client.model.PrintableImageModel;
import com.dsteiner.widgets.client.model.RateItModel;
import com.dsteiner.widgets.client.model.StarModel;
import com.dsteiner.widgets.client.resources.WidgetsImageBundle;
import com.dsteiner.widgets.client.resources.WidgetsResources;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class RateItWidget extends LazyWidget{

    private class Star extends LazyWidget{

		private final StarModel starModel;
		private StarController starController = new StarController();
        public Star(StarModel starModel) {
            this.starModel = starModel;
        }
		@Override
		public Widget createContentWidget() {
			AbstractImagePrototype normalImg = WidgetsImageBundle.Util.getInstance().starNormal();
			AbstractImagePrototype selectedImg = null;
			int rate = starModel.getRate();
	        switch (rate) {
			case 1:
				selectedImg = WidgetsImageBundle.Util.getInstance().starSelected1();
				break;
			case 2:
				selectedImg = WidgetsImageBundle.Util.getInstance().starSelected2();
				break;
			case 3:
				selectedImg = WidgetsImageBundle.Util.getInstance().starSelected3();
				break;
			case 4:
				selectedImg = WidgetsImageBundle.Util.getInstance().starSelected4();
				break;
			case 5:
				selectedImg = WidgetsImageBundle.Util.getInstance().starSelected5();
				break;
			}
	        
			AbstractImagePrototype rate1Img = WidgetsImageBundle.Util.getInstance().starRated1();
			AbstractImagePrototype rate2Img = WidgetsImageBundle.Util.getInstance().starRated2();
			AbstractImagePrototype rate3Img = WidgetsImageBundle.Util.getInstance().starRated3();
			AbstractImagePrototype rate4Img = WidgetsImageBundle.Util.getInstance().starRated4();
			AbstractImagePrototype rate5Img = WidgetsImageBundle.Util.getInstance().starRated5();
	        
			
			PrintableImageModel printableImageModel = new PrintableImageModel();
			ImageExt image = new ImageExt();
			
			this.setStyleName("stars");
			
            rateItController.registerStarModel(starModel);
            starController.registerStarModel(starModel);
            starController.registerImage(image);
            starController.registerNormalImage(normalImg);
            starController.registerRate1Image(rate1Img);
            starController.registerRate2Image(rate2Img);
            starController.registerRate3Image(rate3Img);
            starController.registerRate4Image(rate4Img);
            starController.registerRate5Image(rate5Img);
            starController.registerSelectedImage(selectedImg);
            starController.registerStar(this);
            starController.registerRateItModel(rateItModel);
			return image;
		}
    }

    private final HTMLExt rateComment = new HTMLExt("");
    private WidgetsImageBundle imageBundle = WidgetsImageBundle.Util.getInstance();
	private RateItController rateItController = new RateItController();
	protected RateItModel rateItModel;

    public RateItWidget(RateItModel rateItModel) {
		this.rateItModel = rateItModel;
    }
    
	@Override
	public Widget createContentWidget() {
		FlowPanel flowPanel = new FlowPanel();
		HorizontalPanel mainPanel = new HorizontalPanel();
        InlineHTMLExt messageHtml = new InlineHTMLExt();
        
		for (int i = 1; i < 6; i++) {
			StarModel starModel = new StarModel();
			starModel.setRate(i);
			Star star = new Star(starModel);
			mainPanel.add(star);
		}
		
        flowPanel.add(mainPanel);
		flowPanel.add(messageHtml);
		
        rateComment.setStyleName("textLabelSide");
        addStyleName(WidgetsResources.INSTANCE.css().rateitWidget());
        messageHtml.addStyleName(WidgetsResources.INSTANCE.css().messageHtml());
        flowPanel.setStyleName("flow-panel");
        
		rateItController.registerRateItWidget(this);
        rateItController.registerRateItModel(rateItModel);
        rateItController.registerMessageHtml(messageHtml);
        return flowPanel;
	}
    
}
